package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "net/http"
    "time"
)

// Monitor 包装handler function， 不侵入业务逻辑
func Monitor(h http.HandlerFunc) http.HandlerFunc {
    return func(resp http.ResponseWriter, req *http.Request) {
        start := time.Now()
        h(resp, req)
        duration := time.Since(start)

        // counter 类型， metric的记录方式
        WebRequestTotal.With(prometheus.Labels{"method": req.Method, "endpoint": req.URL.Path}).Inc()
        // histogram类型， metric的记录方式
        WebRequestDurationSeconds.With(prometheus.Labels{"method": req.Method, "endpoint": req.URL.Path}).Observe(duration.Seconds())
    }
}
